//
//  ExpensePopoverViewController.swift
//  NewCard
//
//  Created by jyh on 2017/11/30.
//  Copyright © 2017年 yihui. All rights reserved.
//

import UIKit

class ExpensePopoverViewController: UIViewController {

    var models = [CardModel]()
    // 银行名称
    @IBOutlet weak var bankNameLabel: UILabel!
    // 银行卡号
    @IBOutlet weak var bankCardLabel: UILabel!
    // 固定额度
    @IBOutlet weak var totalLabel: UILabel!
    // 可用额度
    @IBOutlet weak var usableAmountLabel: UILabel!
    // 临时额度
    @IBOutlet weak var temporaryAmountLabel: UILabel!
    // 总欠款
    @IBOutlet weak var oweLabel: UILabel!
    // 本期偿还
    @IBOutlet weak var thisReimbursementLabel: UILabel!
    // 账单日
    @IBOutlet weak var billDateLabel: UILabel!
    // 还款日
    @IBOutlet weak var returnDateLabel: UILabel!
    // 账单周期
    @IBOutlet weak var billingCycleLabel: UILabel!
    

    override func viewDidLoad() {
        super.viewDidLoad()

        view.layer.cornerRadius = 10
        loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    
    deinit {
        //HHLog("销毁")
    }
    
}

extension ExpensePopoverViewController {
    
    fileprivate func loadData() {
        
        let array = OpenData.loadData(CardDetailsSelectData: models.last!)
        var tempArr = [CardModel]()
        for arr in array! {
            tempArr.append(CardModel(dict:arr))
        }
        cardData(tempArr)
    }
    
    // 设置银行卡基础信息
    private func cardData(_ data : [CardModel]) {
        let _data = data.last!
        bankNameLabel.text = _data.bankName
        bankCardLabel.text = String(_data.bankCard)
        totalLabel.text = String(_data.total)
        usableAmountLabel.text = String(_data.usableAmount)
        temporaryAmountLabel.text = String(_data.temporaryAmount)
        oweLabel.text = String(format: "%.2f", (_data.total - _data.usableAmount))
        billDateLabel.text = String(_data.billDate)
        returnDateLabel.text = String(_data.returnDate)
        statistics(_data)

    }
    
    // 统计信息
    private func statistics(_ data : CardModel) {
        
        let _data = ExpenseModel()
        let fDate = thisReimbursement(data.billDate, data.returnDate)
        _data.bankCard = data.bankCard
        _data.bankName = data.bankName
        _data.startDate = fDate.0
        _data.endDate = fDate.1
        let arrs = OpenData.loadData(ExpenseStartAndDateEndDate: _data)

        var expenseArray = [ExpenseModel]()
        for arr in arrs! {
            expenseArray.append(ExpenseModel(dict: arr))
        }
        
        var money : Double = 0
        for arr in expenseArray {
            money += arr.money
        }
        
        thisReimbursementLabel.text = String(money)
        billingCycleLabel.text = fromDate(fDate.0, 5) + " -- " + fromDate(fDate.1, 5)
        
    }
    
    // 获取查询账单日周期时间段
    private func thisReimbursement(_ billDate : Int,_ returnDate: Int) -> (String,String) {
        
        let startDay = String(format: "%02d", billDate + 1)
        let endDay = String(format: "%02d", billDate)
        let currentdate = Date()
        let calendar = Calendar.current
        let dateComponents = calendar.dateComponents([.year,.month], from: currentdate)
        let startMonth = billDate < returnDate ? (dateComponents.month! - 1) : (dateComponents.month! - 2)
        let endMonth = billDate < returnDate ? dateComponents.month! : (dateComponents.month! - 1)
        let startDate = String(dateComponents.year!) + "-" + String(startMonth) + "-" + startDay
        let endDate = String(dateComponents.year!) + "-" + String(endMonth) + "-" + endDay
        return (startDate,endDate)
        
    }
    
    // 截取要显示的账单周期
    func fromDate(_ str : String,_ num : Int) -> String {
        let index = str.index(str.startIndex, offsetBy: 5)
        return str.substring(from: index)
    }
    
}
